//
//  CouponViewController.swift
//  ShopIOS
//
//  Created by Apple on 2019/8/2.
//  Copyright © 2019 Apple. All rights reserved.
//

import UIKit
import SnapKit
import Tabman
import Pageboy

class CouponViewController: TabmanViewController,PageboyViewControllerDataSource, TMBarDataSource {
    @IBOutlet weak var uv_statubar: UIView!
    @IBOutlet weak var uv_back: UIView!
    @IBOutlet weak var uv_tab: UIView!
    var userCouponResult:UserCouponResult!
    var vcs:[UIViewController]! = []
    var index:Int!
    var titles = ["可用优惠券（0）","不可用优惠券（0）"]
    
    var nouseList=[UserCoupon]()
    var useList=[UserCoupon]()
    
    init(index:Int){
        super.init(nibName: nil, bundle: nil)
        self.index=index
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        uv_statubar.snp.remakeConstraints { (make) in
            make.height.equalTo(kStatusBarHeight)
        }
        uv_back.addOnClickListener(target: self, action: #selector(back))

        print(kStatusBarHeight)
        self.additionalSafeAreaInsets=UIEdgeInsets(top: 90, left: 0, bottom: 0, right: 0)
        
        loaddata()
    }
    @objc func back(){
        self.navigationController?.popViewController(animated: true)
    }
    func loaddata(){
        var d = ["app_key":app_key,
            "userkey":userkey!,
            "timestamp":getTimestamp()
            ] as [String : String]
        let sign=SignTopRequest(params: d)
        d["sign"]=sign
        AlamofireHelper.get(url: GetUserCounponList, parameters: d, successHandler: {[weak self](res)in
            HUD.dismiss()
            guard let ss = self else {return}
            ss.userCouponResult=UserCouponResult.init(json: res)
            ss.titles[0] = "可用优惠券（\(ss.userCouponResult.NoUseCount)）"
            ss.titles[1] = "不可用优惠券（\(ss.userCouponResult.UserCount)）"
            for coupon in ss.userCouponResult.Coupon{
                print(Date().compare(getDateFromTime(time: coupon.EndTime!)) == .orderedDescending)
                print(Date())
                print(getTimestamp() ?? "")
                print(getTimestampFromDate(date: getDateFromTime(time: coupon.EndTime!)) ?? "")
                if coupon.UseStatus==1||getDateFromTime(time: getTimestamp()).compare(getDateFromTime(time: coupon.EndTime!)) == .orderedDescending{
                    ss.nouseList.append(coupon)
                }else{
                    ss.useList.append(coupon)
                }
            }
            ss.vcs.append(CouponTableViewController(dataSouce: ss.useList))
            ss.vcs.append(CouponTableViewController(dataSouce: ss.nouseList))
            ss.dataSource = ss
            
            // Create bar
            let Tbar = TMBar.ButtonBar()
            Tbar.layout.transitionStyle = .progressive
            Tbar.buttons.customize { (button) in
                button.tintColor = .black
                button.selectedTintColor = .red
                button.font=UIFont.systemFont(ofSize: 13)
            }
            
            Tbar.indicator.tintColor = .red
            Tbar.layout.contentMode = .fit
            
            ss.addBar(Tbar, dataSource: ss, at: .custom(view: ss.uv_tab, layout: nil))
            
        }){[weak self] (error) in
            HUD.dismiss()
            guard let ss = self else {return}
        }
    }
    
    func numberOfViewControllers(in pageboyViewController: PageboyViewController) -> Int {
        return vcs.count
    }
    
    func viewController(for pageboyViewController: PageboyViewController, at index: PageboyViewController.PageIndex) -> UIViewController? {
        return vcs[index]
    }
    
    func defaultPage(for pageboyViewController: PageboyViewController) -> PageboyViewController.Page? {
        return PageboyViewController.Page.at(index: self.index)
    }
    
    func barItem(for bar: TMBar, at index: Int) -> TMBarItemable {
        return TMBarItem(title: titles[index])
    }
}
